#include <QGridLayout>
#include "abstract-section-view.h"
#include "debug-helper.h"
#include "graph-node.h"
#include "meta-types.h"
#include "navigator.h"
#include "navigator-bar.h"
#include "section-graph-creator.h"
#include "section-info.h"
#include "section-view-factory.h"
#include "section-view-manager.h"
#include "ui_section-view-main-window.h"

SectionViewManager::SectionViewManager(QWidget *parent) :
    QWidget(parent), m_sectionViewGraph(0), m_currentSectionView(0), m_navigatorBar(0),
    m_navigator(0), m_gridLayout(0)
{
}

SectionViewManager::~SectionViewManager()
{
    delete m_sectionViewGraph;
}

void SectionViewManager::initialize()
{
    Q_ASSERT(!m_gridLayout);
    m_gridLayout = new QGridLayout(this);
    m_gridLayout->setContentsMargins(0, 0, 0, 0);
    Q_ASSERT(!m_navigatorBar);
    m_navigatorBar = new NavigatorBar(this);
    m_navigator = m_navigatorBar->navigator();
    m_navigatorBar->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Maximum);
    connect (m_navigatorBar, &NavigatorBar::activated, this, &SectionViewManager::showSectionFromNavigator);

    SectionGraphCreator sectionGraphCreator;
    m_sectionViewGraph = sectionGraphCreator.createBeginningGraph();
    showSection(m_sectionViewGraph, SectionViewRequest, false);
}

void SectionViewManager::showSection (GraphNode<SectionInfo>* sectionNode, RequestOrigin requestOrigin,
                                      bool isSiblingNode)
{
    if (m_currentSectionView)
    {
        if (m_currentSectionView->sectionNode() == sectionNode)
        {
            m_currentSectionView->refresh();
            return;
        }
        else
        {
            m_navigatorBar->setParent(this);
            m_gridLayout->removeWidget(m_currentSectionView);
            m_currentSectionView->hide();
            m_currentSectionView->deleteLater();
        }
    }

    m_currentSectionView = SectionViewFactory::sectionView(sectionNode->data().section());
    if (!m_currentSectionView)
        return;

    m_currentSectionView->setWindowFlags(Qt::Widget);
    m_currentSectionView->setSectionNode(sectionNode);
    m_currentSectionView->setNavigatorBar(m_navigatorBar);
    connect (m_currentSectionView, &AbstractSectionView::notifyHint, this, &SectionViewManager::notifyHint);
    connect (m_currentSectionView, &AbstractSectionView::notifyInformation, this, &SectionViewManager::notifyInformation);
    connect (m_currentSectionView, &AbstractSectionView::notifyWarning, this, &SectionViewManager::notifyWarning);
    connect (m_currentSectionView, &AbstractSectionView::showSectionRequested,
             this, &SectionViewManager::showSectionFromSectionView);
    m_currentSectionView->initialize();
    m_gridLayout->addWidget(m_currentSectionView);

    if (requestOrigin == NavigatorRequest)
        return;

    QVariant sectionNodeVariant;
    sectionNodeVariant.setValue(sectionNode);
    if (isSiblingNode)
    {
        m_navigator->addElementToPosition(m_navigator->currentPosition(),
                     sectionNode->data().description(), sectionNodeVariant);
    }
    else
    {
        m_navigator->addElement(sectionNode->data().description(), sectionNodeVariant);
    }
}

void SectionViewManager::showSectionFromSectionView (GraphNode<SectionInfo>* sectionNode, bool isSiblingNode)
{
    showSection(sectionNode, SectionViewRequest, isSiblingNode);
}

void SectionViewManager::showSectionFromNavigator (const QVariant& data)
{
    GraphNode<SectionInfo>* sectionNode = data.value<GraphNode<SectionInfo>*>();
    showSection(sectionNode, NavigatorRequest, false);
}
